package com.naiterui.ehp.bs.doctor.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.doctor.product.CategoryBO;
import com.naiterui.ehp.bp.bo.doctor.product.CueWordBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductDetailBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductSearchParamBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.remote.IAdRemoter;
import com.naiterui.ehp.bs.doctor.service.IProductService;
import com.naiterui.ehp.bs.doctor.vo.param.ProductSearchParam;
import com.naiterui.ehp.bs.doctor.vo.product.CategoryVO;
import com.naiterui.ehp.bs.doctor.vo.product.CueWordVO;
import com.naiterui.ehp.bs.doctor.vo.product.ProductDetailVO;
import com.naiterui.ehp.bs.doctor.vo.product.ProductInfoVO;

import cn.hutool.core.bean.BeanUtil;

/**
 * @author fwd
 */
@Service
public class AdProductServiceImpl implements IProductService {

    @Autowired
    private IAdRemoter remoter;

    @Override
    public PageVO<ProductInfoVO> searchProduct(ProductSearchParam productSearch) throws BusinessException {
        ProductSearchParamBO searchParam = ProductSearchParamBO.builder().doctorId(productSearch.getDoctorId())
            .key(productSearch.getKey()).categoryId(productSearch.getCategoryId())
                                                               .num(productSearch.getNum()).page(productSearch.getPage()).build();

        PageVO<ProductInfoVO> pageVO = remoter.getProductInfo(searchParam);
        //处理客户端模型转换问题
        pageVO.getResult().forEach(s -> {
            s.setStockNum(s.getQuantity());
            s.setQuantity(1L);
        });
        return pageVO;
    }

    @Override
    public ProductDetailVO getProductDetailInfo(Long doctorId, Long skuId) throws BusinessException {
        // 远程调用商品详情
        ProductDetailBO productDetailInfo = remoter.getProductDetailInfo(skuId);

        // VO组装
        ProductDetailVO productDetail = new ProductDetailVO();
        if (productDetailInfo != null) {
            BeanUtil.copyProperties(productDetailInfo, productDetail);
        }

        return productDetail;
    }

    @Override
    public List<CueWordVO> getCueWord(Long doctorId, String key) throws BusinessException {
        // 调用b2c 获取联想词
        List<CueWordBO> productCueWord = remoter.getProductCueWord(key);

        List<CueWordVO> cueWords = new ArrayList<>();

        if (productCueWord == null || productCueWord.isEmpty()) {
            return cueWords;
        }
        productCueWord.forEach(cueWord -> cueWords.add(new CueWordVO(cueWord.getName())));

        return cueWords;
    }

    @Override
    public List<CategoryVO> getCategoryInfo(Long doctorId) throws BusinessException {
        List<CategoryVO> categoryVOS = new ArrayList<>();

        // 调用b2c获取分类
        List<CategoryBO> categoryBOS = remoter.getCategoryList();

        if (categoryBOS == null || categoryBOS.isEmpty()) {
            return categoryVOS;
        }

        // 组装VO
        categoryBOS.forEach(category -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOS.add(categoryVO);
        });

        return categoryVOS;
    }
}
